<?php

class CreateSiteForm extends CFormModel
{
	public $name;
	public $cat1;
	public $cat2;
	public $owner_id;
	public $uid;
	public $lat;
	public $lng;
	
	public $link;
	
	public $success;
	public $errors;
	
	private $place;
	private $like;
	private $comment;
	
	private $site;
	private $style;
	
	public function rules()
	{
		return array(
			array('name, cat1, cat2, owner_id, woid, lat, lng, link','required'),
		);
	}
	
	public function create()
	{
		$transaction = Yii::app()->db->beginTransaction();
		try {
			$this->place = new Place;
			$this->place->attributes = $this->attributes;
			if(!$this->place->save()){
				throw new Exception('Place save fail.');
			}
			$this->site = new Site;
			$this->site->place_id = $this->place->place_id;
			$this->site->link = $this->link;
			$this->site->gallery = '1';
			$this->site->logo = '1';
			$this->site->style = '1';
			$this->site->tab = '1';
			if(!$this->site->save()){
				throw new Exception('Site save fail.');
			}
			$transaction->commit();
			$this->success = true;
		}
		catch (Exception $e) {
			$transaction->rollback();
			$this->errors = array($e->getMessage(),$this->place->errors,$this->site->errors);
			$this->success = false;
		}
	}
}